<?php
include_once '../samepart/header.php';
?>
<title>No.2商城-个人中心</title>

<div id="dingdanxiangqing_body">
    <div id="dingdanxiangqing_body_big">
        <div id="big_left">
            <p style="font-size:18px;margin-top: 15px">订单中心</p>
            <a id="big_left_a" href="../order/orderList.php">我的订单</a><br/>
            <!--               <a id="big_left_a" href="">意外保</a><br/>-->
            <!--               <a id="big_left_a" href="">团购订单</a><br/>-->
            <!--               <a id="big_left_a" href="">评价晒单</a><br/>-->
            <!--               <p style="font-size:18px">个人中心</p>-->
            <!--               <a id="big_left_a" href="">我的个人中心</a><br/>-->
            <!--               <a id="big_left_a" href="">消息通知</a><br/>-->
            <!--               <a id="big_left_a" href="">优惠券</a><br/>-->
            <!--               <a id="big_left_a" href="">收货地址</a><br/>-->
        </div>
        <div id="big_right" style="height: 500px;overflow: scroll;">
            <div style="margin:0 20px;">
                <h3>收货地址</h3>
                <hr>
                <table id="tb_list" class="table table-striped table-hover table-bordered table-condensed">
                </table>
            </div>
            <br>
            <div class="container" style="width:960px;">
                <form action="userAddressAdd" method="post" class="form-horizontal">
                    <div class="form-group">
                        <label class="col-sm-2 form-label">收件人</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" name="name"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 form-label">手机号</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" name="phone"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="form-label">详细地址</label>
                        <textarea rows="3" class="form-control" name="detail"></textarea>
                    </div>
                    <div class="form-group col-md-12">
                        <input type="button" class="btn btn-primary" value="添加地址" onclick="addA()">
                        <a id="selfBackCart" href="" class="btn btn-primary">返回购物车</a>
                    </div>


                </form>

            </div>
        </div>
    </div>
</div>
<!--                存放变量-->
<input type="text" name="vvalue" value="" style="display: none"/>
<!-- 底部 -->

<!--网站版权部分开始-->
<?php include_once '../samepart/footer.php' ?>

<script>
    $("#selfBackCart").attr("href","../cart/cart.php?show_cart_id="+getCartId())
    /**
     * getUserId
     */
    function getUserId() {
        var uid = '';
        $.ajax({
            async: false,//取消异步执行，为同步执行。进而被其他方法调用
            url: "http://localhost:80/phalapi/public/index.php?s=App.User.Login",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                'username': '<?php echo $_SESSION["username"] ?>',
                "password": '<?php echo $_SESSION["password"] ?>'
            }),
            dataType: "JSON",
            success: function (result) {
                uid = result['data']['userid'];

            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
        return uid;
    }

    /**
     * getCartId
     */
    function getCartId() {
        var carid = ''
        $.ajax({
            async: false,//取消异步执行，为同步执行。进而被其他方法调用
            url: "http://localhost:80/phalapi/public/index.php?s=App.Cart.getCartInfoByUid",
            type: "GET",
            data: {
                "user_id": getUserId()
            },
            dataType: "JSON",
            success: function (result) {
                carid = result.data.info[0].id;

            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
        return carid;
    }

    //获取当前用户
    var uid;

    $(function () {
        $.ajax({
            url: "http://localhost/phalapi/public/index.php?s=App.User.getUserInfoByUserName",
            type: "GET",
            data: {
                "user_name": '<?php echo $_SESSION["username"]?>'
            },
            dataType: "JSON",
            success: function (result) {
                // getOrderInfo(result.data.info.id);
                uid = result.data.info.id;
                if (uid = '') {
                    alert('请先登录');
                    window.location.replace("../login/login.php");
                }

                $("input[name='vvalue']").val(result.data.info.id);

                fun(result.data.info.id);

            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
    })

    function inits() {
        var s = "<tr class='tr_head'><td>序号</td><td>收件人</td><td>手机号</td><td>详细地址</td></tr>";
        return s
    }

    function adds(s, z, name, phone, detail) {
        s += "<tr><td>" + (z) + "</td><td>" + name + "</td><td>" + phone + "</td><td>" + detail + "</td></tr>";
        return s
    }
    //列出当前用户存在的地址
    // $(
    function fun(uid) {
        $.ajax({
            url: "http://localhost/phalapi/public/index.php?s=App.UserAddress.GetUserAddressByUid",
            type: "GET",
            data: {
                "user_id": uid
            },
            dataType: "JSON",
            success: function (result) {
                var user_data = result.data.info;
                var z = 1;
                var s = inits();

                for (var i = 0; i < user_data.length; i++) {

                    var name = user_data[i]['name'];
                    var phone = user_data[i]['phone'];
                    var detail = user_data[i]['detail'];

                    s = adds(s, z, name, phone, detail);
                    z++;
                    // console.log('sss'+s);
                }

                $("#tb_list").html(s);
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
    }

    // )


    function addA() {
        //添加
        var name = $("input[name='name']").val()
        var phone = $("input[name='phone']").val()
        var detail = $("textarea[name='detail']").val();
        var uid = $("input[name='vvalue']").val()

        if (name != '' && phone != '' && detail != '') {
            runAdd(name, phone, detail, uid);
        } else {
            alert('请填写完整');
        }

        function runAdd(name, phone, detail, uid) {
            $.ajax({
                url: "http://localhost/phalapi/public/index.php?s=App.UserAddress.InsertUserAddressInfo",
                type: "POST",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    "userAddress_name": name,
                    "userAddress_phone": phone,
                    "userAddress_detail": detail,
                    "userAddress_uid": uid
                }),
                dataType: "JSON",
                success: function (result) {
                    var data = result.data.info;
                    alert('添加地址成功');
                    window.location.replace("self_info.php");
                },
                error: function (msg) {
                    alert("错误信息：" + msg);
                }
            })
        }
    }

</script>
</body>

</html>